package com.ww.yaodao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ww.yaodao.mapper.TypeMapper;
import com.ww.yaodao.model.bo.TypesDO;
import com.ww.yaodao.model.vo.type.TypeRespVO;
import com.ww.yaodao.model.vo.type.TypeVO;
import com.ww.yaodao.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;

@Service("typeService")
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List getAllType() {
        List<TypesDO> types = typeMapper.selectList(null);
        return types;
    }

    @Override
    public TypeRespVO delete(Integer id) {
        LambdaQueryWrapper<TypesDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypesDO::getTid, id);
        return TypeRespVO.builder().row(typeMapper.delete(wrapper)).build();

    }

    @Override
    public TypeRespVO insertType(TypeVO typeVO) {
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        TypesDO typesDO = new TypesDO();
        if (!StringUtils.isEmpty(typeVO) &&
                !StringUtils.isEmpty(typeVO.getTdate()) &&
                !StringUtils.isEmpty(typeVO.getTname()) &&
                !StringUtils.isEmpty(typeVO.getType())) {

            BeanUtils.copyProperties(typeVO, typesDO);
            typesDO.setTdate(simpleDateFormat.format(typeVO.getTdate()));
            int insert = typeMapper.insert(typesDO);
            return TypeRespVO.builder().row(insert).code(200).build();
        }
        return TypeRespVO.builder().code(101).build();
    }
}
